import type { DeckInfoCardItem } from '/@/api/currentDeck/model/deckModel';
import { defineStore } from 'pinia';
import { store } from '/@/store';
import { cloneDeep } from 'lodash-es';

interface IState {
  gameId: string; // 游戏ID
  isAgainSearch: boolean; // 是否重新搜索
  selectedCards: DeckInfoCardItem[]; // 所选择的卡牌
}

interface IGetter {
  getGameId: (state: IState) => string;
  getIsAgainSearch: (state: IState) => boolean;
  getSelectedCards: (state: IState) => DeckInfoCardItem[];
  [propName: string]: ((state: IState) => IState | unknown) | (() => void);
}

interface IActions {
  setGameId: (gameId: string) => void;
  setIsAgainSearch: (isAgainSearch: boolean) => void;
  setSelectedCards: (card: DeckInfoCardItem[]) => void;
}

export const useDeckSetStore = defineStore<'app_deck_set', IState, IGetter, IActions>({
  id: 'app_deck_set',
  state: () => {
    return {
      gameId: '',
      isAgainSearch: false,
      selectedCards: [],
    };
  },
  getters: {
    getGameId: (state) => {
      return state.gameId;
    },
    getIsAgainSearch: (state) => {
      return state.isAgainSearch;
    },
    getSelectedCards: (state) => {
      return state.selectedCards;
    },
  },
  actions: {
    setGameId(gameId: string) {
      this.gameId = gameId;
    },
    setIsAgainSearch(isAgainSearch) {
      this.isAgainSearch = isAgainSearch;
    },
    setSelectedCards(cards) {
      this.selectedCards = cloneDeep(cards);
    },
  },
});

// Need to be used outside the setup
export function useDeckSetStoreWithOut() {
  return useDeckSetStore(store);
}
